package cn.gok;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.servlet.http.HttpSession;

/**
    @SessionAttributes(value = "userInfo")
        表示将当前Controller类中，mode数据模型中，key为userInfo的数据存入session中
 */
@Controller
@SessionAttributes(value = "userInfo")
public class TestControllerSessionAttribute {

    @RequestMapping("/testSessionAttributes_login")
    public String login(String userName, String password, ModelMap modelMap){
        System.out.println("userName: " + userName);
        System.out.println("password： " + password);

        User user = new User();
        user.setUserNamex(userName);
        user.setPassword(password);
        user.setGender("男");

        //存入数据模型中
        modelMap.addAttribute("code","200");
        modelMap.addAttribute("msg","登录成功");
        modelMap.addAttribute("userInfo",user);

        return "success";
    }


    @RequestMapping("/testSessionAttributes_getUserInfo")
    public String getUserInfo(HttpSession httpSession){
        User userInfo = (User) httpSession.getAttribute("userInfo");
        System.out.println(userInfo);
        return "success";
    }
}
